// pages/fabushop/fabushop.js
const db = wx.cloud.database().collection('fankui')
Page({

  data: {
    imageUrl: '',
    tempFilePaths: '',
    content: '',
    wenti: ''
  },

  onLoad: function (options) {

  },
  handleinput (data) {
    this.setData ({
      wenti: data.detail.value
    })
  },
  handletext (data) {
    this.setData ({
      content: data.detail.value
    })
  },
  upImage () {
    wx.showLoading({
      title: "上传中...." ,
      mask: true,
  })
    let thiss = this
    wx.chooseImage({
      count: 4,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        thiss.setData ({
          tempFilePaths: tempFilePaths
        })
        wx.cloud.uploadFile({
          cloudPath: thiss.data.wenti, // 上传至云端的路径
          filePath: tempFilePaths[0], // 小程序临时文件路径
          success: res => {
            console.log(res)
            thiss.setData ({
              imageUrl: res.fileID
            })
            wx.hideLoading()
          },
          fail: console.error
        })
      }
    })
  },
  addData () { // 添加数据
    db.add({
      data: {
        content:this.data.content,
        image:this.data.imageUrl,
        wenti:this.data.wenti,
        user:wx.getStorageSync("userInfo").nickName
      },
      success (res) {
        wx.navigateBack({
          delta: 1
      })
      },
      fail (err) {
        console.log(err)
      }
    })
  },
})